package com.yfbao.horizon.demo;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadInfoTest {

    private  static ExecutorService executorService2 = Executors.newFixedThreadPool(10);

    public static void main(String[] args) {
        for(int i=0;i<100;i++){
            executorService2.submit(()->{
                System.out.println("aaa");
            });
        }

        tt();
    }

    private static void tt(){
        ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
        try {
            long[] tids = tmbean.getAllThreadIds();
            ThreadInfo[] tinfos = tmbean.getThreadInfo(tids, Integer.MAX_VALUE);

            List<ThreadInfo> tinfoList = new ArrayList<ThreadInfo>();
            Map<Thread.State, Integer> threadGroup = new HashMap<Thread.State, Integer>();
            for (Thread.State state : Thread.State.values()) {
                threadGroup.put(state, 0);
            }
            for (int i = 0; i < tinfos.length; i++) {
                if (tinfos[i] != null) {
                    threadGroup.put(tinfos[i].getThreadState(), threadGroup.get(tinfos[i].getThreadState()) + 1);
                    tinfoList.add(tinfos[i]);
                }
            }
            //获取
            System.out.println("size:"+tinfoList.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
